<?php
namespace BaseExt;

use BaseExt\AliPay\aop\request\AlipayTradeAppPayRequest;
use BaseExt\AliPay\aop\AopClient;

include("AliPay/AopSdk.php");
/**
 * PicUpload Class
 */
class AliPay {
    //开发者私钥
    private $rsaPrivateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCV/mHDGBILcJFXsZQfs+OzVSZQTGKqVjX5GCQWr+H9VQXSA1L1zqqNs+PvoN0G6yepEB1CM1KiPskhvfS0faNiwAydFd6ZrPy/pbGQjbmvt97QM7Jd5w9D94czz1zXkh+mHw3LXj94VC9z9Qpe6I6vd1Poelm2DjWQGEtC/YNz/hr58/WLA40Z/hyILAzarhA0Xvy3/WwWG1Fxv7ERlE1nJZTTJdO4I94+nTSGKvaCIwWarXImloyj/WMy+4ZTmm2ppRDfxe7vxtKJuXcyUwcMXgIDr0sgjUu//QOj+ABDONwKZHMsozw8sOx8xn3toqXsmx3iNnr5G3zNmyv5i7k5AgMBAAECggEAGDUa8BKlbuU2/9EwF8G/oQTADzuUvOr3AQGtaV/veCRNsSxKKlO1YPNEeTscEO7I0tc/8Zw43OiHK4Ptx2dzhA98ual66JBrpj6Gns7QxhRFyJJfWhpdc1myn2D7GeuCYhwm7MiEM9ZaZjuI9fJQfO8kQqPkcKZwbJYPJ3ukyIZd4snHVtkXe+1c87GFiYlVs/oAQwL+Rfq56IFoVWE5aN5EHrfUFDBpYpnPuY/aPfy1NYccrVGCmc7xoEzrz4+CPgKfC64UxBq8HIHCUL98UiyWQagST5spmy0ntY/cobyeQv3NvFGgaGiKcT1gjuACLaIVHGeBm1BMOkR/QEunkQKBgQDMmdjTstjZl1RaLfx8WQZD93+xlD0joyDSYvcyJxhTtUfkeBFk9O0YSWaKjkQasZGc9wL/QIew1031/BD6c4VRb4kqRaKbOdRNLDrBJ7Fb1D7MktHvW5Bi2Q//d+ZEtrEdAl8Rqeb1zplPAt9T36fzR7snOS73Fxh2QyTCijKCJQKBgQC7rKtuHliuBA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FRnxbogP+u2QIDAQAB';

    public function createOrder($sn,$price,$type = 0,$info = "订单支付")
    {
//        $sn = "sn001";
//        $price = 1;

        $aop = new AopClient();

        //沙箱环境
//        $aop->gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
//        $aop->appId = "2018022702280079";
        //正式环境
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = "2018041002531952";



//        $aop->rsaPrivateKey = 'MIIEowIBAAKCAQEArIaT35vprdrZFU3Th7tuNZzUop4tAMcJ3Yv1qNr9XlClFIHIvX6bDG42B0lZWM9F9KHe9Wr5eAUGObrg7xiQ1K36GL2QMKVNwkyVmYaQIScl7n9ddcOz1dfUdaa8fDuhy3+KvBE0VgNN9HXv0wxnJnwoWSPnLom9qqBvGoMNytGcsEEGPT+ImjZxByPPcEFFYyfgwRfgaQ1if+F7VFPSUQgEmiPj+Te2CgnPnI2r/ZRPn9lUpt149zJfjU/OCK7U3WztvRxtcX2v6qn+afbVVnrXAkR4Ge3CJYFDPVSjPkwGpyAD8tN+dhGuh3GdGJ/4gwSi65TNJYllBHgqE6fwmQIDAQABAoIBAQCm1OR94PkRY4V0cnPQ11B1i5ibQDXogJBrqoH9rPZBk95ufCowBBPO62xXk3hQxu6w5y8BR1bB1qn0fWAyFSFIaIxmDDk4sUEGWKR5d4Q8auzlOkiYLozihvkuY/P0dn24mTcBMenYGw3xDXLCNUYyEvG5cVyGxi+gpc+KZZyauI0sz/dxnH5+aoGZlfp29l/QyD1LXI5sNy3wdqSjrOtYiJbm/SgfAazY9Jqk+stqLgFSPRcmsTbDYKlXffv6KiXWfCalBuS9izXtmjAjCHyiHHcaY6dmMDzaZyiyzHgVFgghqyGGW5WEXFuaFbg8ADXuQMBlDe/2OXH+JYj07Q0BAoGBANbtJVPm29UDZjoSCORmxJMOz8a3+zkqfdzV7hOT9l9TroUBOuU/HkFyrRMdaUarxWQeLi5SDuQu2pM8LC+lFPXtxfU1VEu1BgQZxx8EqexrS+j++K1uP6eQRM9IjcvpdfK8E6hBuCPhj15jBX8NNIxd6Otw5hqVlvMZWyUS9nIpAoGBAM1/D+JSCw7cu7XGA6RqHe0wRWYBFw4vJcA8/JYwOIxT4mAhlz86rYtlUrAISz8HdZmsUOjVqnhbstZ0m9hEPvEBoMs8ESi05cpV0WgnCGrPkJWFFzClCmT2xByIgUkHfsuSXBfi+3VWix6NK2tUZUhccFFQDE4XReiToKWH8bjxAoGAG4SbfeWWCez4fsDmYJAD5SxFUBUoVkZpfQfrBpiwN75YHvFsYAO31Ptgy4y9eO3vRQVUNlwNSgvxsl5G7ga960r5SI7+bv8LeSTvyLnpI7sNKhTTNClkfwjKtpt8hDos/tozzhwz/mwX1ibpgj3nl31CoDqBS7ARdqitcc6+vRkCgYAtHI6tHVumGJjwj5avGkiVkDo76VNZXspHyFue0NkpHSUd+2HLlKMSbdzYHgn+vTF42dYtNvKkV2UMJS8tK9YxbC0TUI1nxUaIw4wj9CWp5QUVfdEYcbK1dWKZ7PGMyaNxJyEmcULIgIYr38u/VRii6QATKaJ55iwHoZjrnctX0QKBgDoVoDbLC112CrjazhVv90rO8u9/kpAsypnaEMzuoATLmod/8n6XHTkH2easG4Nvo8fBZILUMFL0fR2qfV/ork0Uf8JC0vvhrCtxxTzNp+6DZlKBPY8ICxJq7973DUJnSbm06YYD2l7bNaYK+hN0/LRCRgOPVBYLQpmF9J4nGuUg';
//        开发者私钥
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        //支付宝公钥
//        $aop->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnvo2cNdlcVem4ZRDuSqwL+Z4PLgjJlwGAwAj3MkwDYoyYmj4Jx/FN9LR6fqE44WJ+jKJOk8AOechdGrTzI7dP6Zu7/aLg3AmtJJYb9fu9M6vn0jFME52wex5jgtNDAZU9yBBEJH3ihJpHqtxfF6WY9KSDDOeUwL9cn8APEz1Ean8lBQQ4gdvv2JY2tBWZ29TvLRFH+ms02UEdhV+x1JM9fYjnONAzlu7SKmhRJK/W9p1iwZ8YmTSNZCNILcIW2uJrAUQ7XNIA9Abxok5FM4ecS3vB/ApCvg8fKRAWtK7EJ241ib8dOet3GIIi1NcdStRYMTvTdBgLe3u6S7PRvNDswIDAQAB';
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数
        $price = $price / 100;
        $bizcontent = "{\"body\":\"用户正在支付订单\","
            . "\"subject\": \"$info\","//这一行是订单支付的信息
            . "\"out_trade_no\": \"$sn\","
            . "\"passback_params\": \"$type\","
            . "\"timeout_express\": \"30m\","
            . "\"total_amount\": \"$price\","
            . "\"product_code\":\"QUICK_MSECURITY_PAY\""
            . "}";
       // $request->setNotifyUrl("http://edu.sano100.com/app_dev.php/money/charge/77a90331fb27755e0361babcfd4f2264");
        $request->setNotifyUrl("http://edu.sano100.com/app_dev.php/money/charge/8d8e7a734dc529cbecb911d7e5e81644");
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
        return   ($response);//就是orderString 可以直接给客户端请求，无需再做处理。

   }


    public function cash2alipay($account,$money,$rowId)
    {
        $money = ((int)$money)/100;
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018041002531952';
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey= $this->alipayrsaPublicKey;
//        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipayFundTransToaccountTransferRequest();


        $str = "{" .
            "\"out_biz_no\":\"$rowId\"," .
            "\"payee_type\":\"ALIPAY_LOGONID\"," .
            "\"payee_account\":\"$account\"," .
            "\"amount\":\"$money\"," .
            "\"payer_show_name\":\"金粑粑提现\"," .
//                "\"payee_real_name\":\"\"," .
            "\"remark\":\"金粑粑提现\"" .
            "}";

        $request->setBizContent($str);
        $result = $aop->execute ( $request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        $resultMsg = $result->$responseNode->msg;
        if(!empty($resultCode)&&$resultCode == 10000){
            return $resultCode;
        } else {
            return json_encode( $result->$responseNode);
        }
    }
}
